﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyNhaSach.QuanLyNhaSachDTO;
using QuanLyNhaSach.QuanLyNhaSachDao;
using QuanLyNhaSach.QuanLyNhaSachBus;

namespace QuanLyNhaSach.DanhSachForm
{
    public partial class frmLoaiSach : Form
    {
        public int m_Ma;
        public frmLoaiSach()
        {
            InitializeComponent();
        }
        public DataTable LayDanhSachLoaiSach()
        {

            string sql = "select * from LoaiSach ";
            DataTable dt = Database.ExcuQuery(sql);
            return dt;
        }
        

        private void button1_Click(object sender, EventArgs e)
        {
            LoaiSachDTO s = new LoaiSachDTO();
            s.TenLoaiSach = txt_TenLoaiSach.Text;
            
            if (LoaiSachBus.ThemLoaiSach(s) == false)
            {
                MessageBox.Show("Sách đã tồn tại trong CSDL");
            }
            DataTable dt = LoaiSachBus.GetLoaiSachAll();
            grid_viewDanhSachLoaiSach.DataSource = dt;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            LoaiSachDTO s = new LoaiSachDTO();
            s.TenLoaiSach = txt_TenLoaiSach.Text;
            LoaiSachBus.Delete(s);
            DataTable dt = LoaiSachBus.GetLoaiSachAll();
            grid_viewDanhSachLoaiSach.DataSource = dt;
        }

        private void Thoát_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmLoaiSach_Load(object sender, EventArgs e)
        {

            DataTable dt = LoaiSachBus.GetLoaiSachAll();
            grid_viewDanhSachLoaiSach.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            LoaiSachDTO s = new LoaiSachDTO();
            s.TenLoaiSach = txt_TenLoaiSach.Text;
            s.MaLoaiSach = m_Ma;
            if (LoaiSachBus.Update(s) == false)
            {
                MessageBox.Show("Sách đã tồn tại trong CSDL");
            }
            DataTable dt = LoaiSachBus.GetLoaiSachAll();
            grid_viewDanhSachLoaiSach.DataSource = dt;
        }

        private void grid_viewDanhSachLoaiSach_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_TenLoaiSach.Text = grid_viewDanhSachLoaiSach["TenLoaiSach", grid_viewDanhSachLoaiSach.CurrentRow.Index].Value.ToString();
            m_Ma = Convert.ToInt32(grid_viewDanhSachLoaiSach["MaLoaiSach", grid_viewDanhSachLoaiSach.CurrentRow.Index].Value.ToString());
        }

        

    }
}
